package com.erp.erp_project.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.erp.erp_project.core.exception.CustomException;
import com.erp.erp_project.mapper.MenuMapper;
import com.erp.erp_project.mapper.PowerMapper;
import com.erp.erp_project.mapper.SubmenuMapper;
import com.erp.erp_project.model.Menu;
import com.erp.erp_project.model.Power;
import com.erp.erp_project.model.Submenu;
import com.erp.erp_project.service.MenuService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * @author yh
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu, MenuMapper> implements MenuService {
    @Autowired
    private SubmenuMapper submenuMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private PowerMapper powerMapper;

    public MenuServiceImpl(MenuMapper mapper) {
        super(mapper);
    }

    @Override
    public List<Submenu> relist(Submenu Submenu,String menufield, String condition){
        if(condition!=null&&!condition.trim().equals("")){
            return submenuMapper.mhselect(menufield,condition);
        }
        return submenuMapper.select(new Submenu());
    }

    @Override
    public void addmenu(String powers, Submenu submenu) throws CustomException {
        Submenu submenu1=new Submenu();
        submenu1.setName(submenu.getName());
        List<Submenu> submenus=submenuMapper.select(submenu1);
        if(submenus.size()!=0){
            for(Submenu submenu2:submenus){
                if(submenu2!=null&&submenu.getName().trim().equals(submenu2.getName().trim())&&submenu.getParent().trim().equals(submenu2.getParent().trim())){
                    throw new CustomException("当前主菜单已存在该次级菜单");
                }
            }
        }


        String[] ps=null;
        if(!powers.equals("[]")){
            List<String> listname=powerMapper.selectpower();
            powers=powers.replace("[","");
            powers=powers.replace("]","");
            powers=powers.replace("\"","");
            ps=powers.split(",");

            for(int m=0;m<ps.length;m++){
                for (int k=m+1;k<ps.length-m;k++){
                    if(ps[m].equals(ps[k])){
                        throw new CustomException("权限名不能相同");
                    }
                }
            }

            for (String st:ps
            ) {
                if(listname.contains(st)){
                    throw new CustomException("当前权限名里<"+st+">已存在");
                }
            }

            Power power=new Power();
            power.setParent(submenu.getName());
            for (int i=0;i<ps.length;i++){
                power.setName(ps[i]);
                powerMapper.add(power);
            }

        }

        Menu menu1=new Menu();
        menu1.setName(submenu.getParent());
        Menu menu=menuMapper.select(menu1);
        if(menu!=null){
            menu1.setId(menu.getId());
            menu1.setCount(menu.getCount()+1);
            menuMapper.update(menu1);
        }else {
            menu1.setCount(1);
            menuMapper.add(menu1);
        }

        if(ps!=null){
            submenu.setPowerCount(ps.length);
            submenuMapper.insert(submenu);
        }else {
            submenu.setPowerCount(0);
            submenuMapper.insert(submenu);
        }

    }

    @Override
    public void delete(String ids){
        String[] sts =ids.split(",");
        List<String> list= Arrays.asList(sts);
        Submenu submenu=new Submenu();
        Menu menu=new Menu();
        for(String ste:list){
            submenu.setId(Integer.parseInt(ste));
            List<Submenu> osmenu=submenuMapper.select(submenu);
            menu.setName(osmenu.get(0).getParent());
            Menu omenu=menuMapper.select(menu);
            if(omenu.getCount()!=0){
                Menu menu2=new Menu();
                menu2.setId(omenu.getId());
                menu2.setCount(omenu.getCount()-1);
                menuMapper.update(menu2);
            }
            powerMapper.delete(osmenu.get(0).getName());
            submenuMapper.delete(ste);
        }

    }

    @Override
    public void updateMenu(Submenu submenu,String rename,String ins,String upids,String dpids,String newins) throws CustomException {
        Submenu submenu1=new Submenu();
        submenu1.setName(submenu.getName());
        List<Submenu> submenus=submenuMapper.select(submenu1);
        if(submenus.size()!=0){
            for(Submenu submenu2:submenus){
                if(submenu2!=null&&submenu.getName().trim().equals(submenu2.getName().trim())&&submenu.getParent().trim().equals(submenu2.getParent().trim())&&!submenu.getId().equals(submenu2.getId())){
                    throw new CustomException("当前主菜单已存在该次级菜单");
                }
            }
        }

        Menu menu=new Menu();
        if(!submenu.getParent().equals(rename)){
            menu.setName(rename);
            Menu omenu=menuMapper.select(menu);
            if(omenu.getCount()!=0){
                omenu.setCount(omenu.getCount()-1);
                menuMapper.update(omenu);
            }

            menu.setName(submenu.getParent());
            Menu omenu2=menuMapper.select(menu);
            if(omenu2==null){
                Menu nmenu=new Menu();
                nmenu.setCount(1);
                nmenu.setName(submenu.getParent());
                menuMapper.add(nmenu);
            }else {
                omenu2.setCount(omenu2.getCount()+1);
                menuMapper.update(omenu2);
            }
        }




        int i=0,j=0;
        List<String> listins=null;
        List<String> listnewins=null;
        String[] stupids=null;
        String[] stdpids=null;
        if(!ins.equals("[]")){
            ins=ins.replace("[","");
            ins=ins.replace("]","");
            ins=ins.replace("\"","");
            String[] strings=ins.split(",");
            listins= Arrays.asList(strings);
            i=strings.length;
        }

        if(!upids.equals("[]")){
            upids=upids.replace("[","");
            upids=upids.replace("]","");
            upids=upids.replace("\"","");
            stupids=upids.split(",");
        }

        if(!dpids.equals("[]")){
            dpids=dpids.replace("[","");
            dpids=dpids.replace("]","");
            dpids=dpids.replace("\"","");
            stdpids=dpids.split(",");
        }

        if(!newins.equals("[]")){
            newins=newins.replace("[","");
            newins=newins.replace("]","");
            newins=newins.replace("\"","");
            String[] strings=newins.split(",");
            listnewins= Arrays.asList(strings);
            j=strings.length;
        }

        if(listins==null&&listnewins!=null){
            for(int m=0;m<listnewins.size();m++){
                for (int k=m+1;k<listnewins.size()-m;k++){
                    if(listnewins.get(m).equals(listnewins.get(k))){
                        throw new CustomException("权限名不能相同");
                    }
                }
            }
        }else if (listins!=null&&listnewins==null){
            for(int m=0;m<listins.size();m++){
                for (int k=m+1;k<listins.size()-m;k++){
                    if(listins.get(m).equals(listins.get(k))){
                        throw new CustomException("权限名不能相同");
                    }
                }
            }
        }else if(listins!=null&&listnewins!=null){
            for(int m=0;m<listins.size();m++){
                for (int k=m+1;k<listins.size()-m;k++){
                    if(listins.get(m).equals(listins.get(k))){
                        throw new CustomException("权限名不能相同");
                    }
                }
                for (int k=0;k<listnewins.size();k++){
                    if(listins.get(m).equals(listnewins.get(k))){
                        throw new CustomException("权限名不能相同");
                    }
                }
            }

            for(int m=0;m<listnewins.size();m++){
                for (int k=m+1;k<listnewins.size()-m;k++){
                    if(listnewins.get(m).equals(listnewins.get(k))){
                        throw new CustomException("权限名不能相同");
                    }
                }
                for (int k=0;k<listins.size();k++){
                    if(listnewins.get(m).equals(listins.get(k))){
                        throw new CustomException("权限名不能相同");
                    }
                }
            }

        }

        submenu.setPowerCount(i+j);
        submenuMapper.updateById(submenu);
        Power power=new Power();
        if(listins!=null){
            for (int k=0;k<stupids.length;k++){
                power.setPowerId(Integer.parseInt(stupids[k]));
                power.setName(listins.get(k));
                power.setParent(submenu.getName());
                powerMapper.updateById(power);
            }
        }
        if(stdpids!=null){
                powerMapper.deletebyids(stdpids);
        }

        Power power1=new Power();
        if(listnewins!=null){
            for (int m=0;m<listnewins.size();m++){
                power1.setName(listnewins.get(m));
                power1.setParent(submenu.getName());
                powerMapper.add(power1);
            }
        }
    }

    @Override
    public  List<Menu> selectnames(Set parentnames){
        return menuMapper.selectnames(parentnames);
    }
}
